package gui;

import org.primefaces.context.RequestContext;

import utils.CurrentUser;
import utils.InvalidInputException;
import utils.UserNotFoundException;

/**
 * Backing bean for managing the settings available to a user
 * 
 * @author Aleksander
 * 
 */
public class UserSettingsBean {

	private String oldPassword;
	private String newPassword1;
	private String newPassword2;
	private String currentRefSystem;
	private String refSystem;

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setNewPassword1(String newPassword1) {
		this.newPassword1 = newPassword1;
	}

	public String getNewPassword1() {
		return newPassword1;
	}

	public void setNewPassword2(String newPassword2) {
		this.newPassword2 = newPassword2;
	}

	public String getNewPassword2() {
		return newPassword2;
	}

	public String getRefSystem() {
		return refSystem;
	}

	public void setRefSystem(String refSystem) {
		this.refSystem = refSystem;
	}

	public String getCurrentRefSystem() {
		if (CurrentUser.getCurrentUser() != null) {
			return CurrentUser.getCurrentUser().getReferenceSystem();
		} else
			return null;
	}

	/**
	 * Changes users reference system
	 */
	public void changeRefSystem() {
		CurrentUser.getCurrentUser().setReferenceSystem(refSystem);
		RequestContext context = RequestContext.getCurrentInstance();
		context.execute("refSysDialog.hide()");
	}

	/**
	 * Changes the password of the user
	 * 
	 * CANT BE TESTED WITH JUNIT
	 * 
	 * @throws UserNotFoundException
	 */
	public void changePassword() {
		MessageController messageController = new MessageController();
		// Check if the old password input field is not empty
		if (oldPassword.isEmpty()) {
			messageController.showErrorMessage("Altes Passwort fehlt!");
			return;
		}
		// Check if the input old password matches the user's current
		// password
		if (!CurrentUser.getCurrentUser().getPassword().equals(oldPassword)) {
			messageController.showErrorMessage("Altes Passwort falsch!");
			return;
		}

		// Check if the new password field and the confirmation field are
		// not empty
		if (newPassword1.isEmpty() || newPassword2.isEmpty()) {
			messageController.showErrorMessage("Neues Passwort fehlt!");
			return;
		}
		// Check if the input in new password field matches the input
		// in thenew password confirmation field
		if (!newPassword1.equals(newPassword2)) {
			messageController.showErrorMessage("Neue Passwörter stimmen");
			messageController.showErrorMessage("nicht überein!");
			return;
		} else {
			try {
				RequestContext context = RequestContext.getCurrentInstance();
				CurrentUser.getCurrentUser().setPassword(newPassword1);
				context.execute("passwordDialog.hide()");
			} catch (InvalidInputException e) {
				messageController
						.showErrorMessage("Neues Passwort muss mindestens");
				messageController.showErrorMessage("eine Länge von 8 Zeichen");
				messageController
						.showErrorMessage("haben und mindestens jeweils");
				messageController
						.showErrorMessage("eine Zahl und ein Sonderzeichen");
				messageController.showErrorMessage("beinhalten!");
			}
		}
	}
}
